﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace NStore.Domain.Violations
{
    public class ProductValidator
    {
        private readonly IEnumerable<Product> _products;

        public ProductValidator(IEnumerable<Product> products)
        {
            _products = products;
        }

        public virtual IEnumerable<RuleViolation> GetRuleViolations(Product product)
        {
            const string propertyName = "Name";
            const string propertyPrice = "Price";

            if (String.IsNullOrEmpty(product.Name))
            {
                yield return new RuleViolation("The Name field is required", propertyName);
            }
            else
            {
                Product productFounded = _products.FirstOrDefault(item => item != product && item.Name.Equals(product.Name));
                if (productFounded != null)
                    yield return new RuleViolation(String.Format("Name duplicated in product with id : {0}", productFounded.Id), propertyName);
            }

            if (product.Price == 0f)
                yield return new RuleViolation("The Price field should have non zero value", propertyPrice);


        }
    }
}
